﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
using System.IO;

namespace WFFS
{
    public partial class FrmGoods : Form
    {
        private Form1 frmMain = null;
        public FrmGoods(Form1 _frm)
        {
            InitializeComponent();
            frmMain = _frm;
        }

        private void FrmGoods_Load(object sender, EventArgs e)
        {
            getInfo();
            timerGoods.Start();
        }

        private void getInfo()
        {
            lvCarInfo.Clear();

            lvCarInfo.BeginUpdate();
            #region 增加Item的標題，共有三個列
            lvCarInfo.View = View.Details;
            lvCarInfo.Columns.Add("车辆名称", 80);
            lvCarInfo.Columns.Add("剩余泡沫", 60);
            lvCarInfo.Columns.Add("剩余油料", 60);
            lvCarInfo.Columns.Add("满载泡沫", 60);
            lvCarInfo.Columns.Add("满载油料", 60);
            lvCarInfo.Columns.Add("泡沫警戒线", 75);
            lvCarInfo.Columns.Add("油料警戒线", 75);
            #endregion

            string sql1 = "select a.*,b.EnterTime,b.OutTime,b.OilSurplus,b.FoamSurplus from CarInfo a " +
                " left join InOutInfo b on a.RFID = b.RFID order by FoamSurplus,OilSurplus ";
            SqlDataReader reader1 = null;
            SqlCommand cmd1 = new SqlCommand(sql1, frmMain.dbConnect);
            reader1 = cmd1.ExecuteReader();

            while (reader1.Read())
            {
                string rfid = reader1["RFID"].ToString();
                string name = reader1["CarName"].ToString();
                string foam = reader1["FoamSurplus"].ToString();
                string oil = reader1["OilSurplus"].ToString();
                string foam1 = reader1["FullFoap"].ToString();
                string oil1 = reader1["FullOil"].ToString();
                string foam2 = reader1["UnderLineFoap"].ToString();
                string oil2 = reader1["UnderLineOil"].ToString();

                lvCarInfo.Items.Add(rfid, name, 0);
                lvCarInfo.Items[rfid].SubItems.Add(foam);
                lvCarInfo.Items[rfid].SubItems.Add(oil);
                lvCarInfo.Items[rfid].SubItems.Add(foam1);
                lvCarInfo.Items[rfid].SubItems.Add(oil1);
                lvCarInfo.Items[rfid].SubItems.Add(foam2);
                lvCarInfo.Items[rfid].SubItems.Add(oil2);
            }
            reader1.Close();

            foreach (ListViewItem lvItems in lvCarInfo.Items)
            {
                string oil = "";
                string oil1 = "";
                string foap = "";
                string foap1 = "";

                oil = lvItems.SubItems[2].Text;//剩余的油
                oil1 = lvItems.SubItems[6].Text;//油标
                foap = lvItems.SubItems[1].Text;//剩余的泡沫
                foap1 = lvItems.SubItems[5].Text;//泡标
                if (oil == "" || foap == "")
                {
                   //
                }
                else
                {
                    if (float.Parse(oil) < float.Parse(oil1) || float.Parse(foap) < float.Parse(foap1))
                    {
                        lvItems.ForeColor = Color.Red;
                    }
                    else
                    {
                        lvItems.ForeColor = Color.Black;
                    }
                }
            }
            lvCarInfo.EndUpdate();
        }

        private void btnRefrse_Click(object sender, EventArgs e)
        {
            getInfo();
        }

        private void FrmGoods_FormClosed(object sender, FormClosedEventArgs e)
        {
            timerGoods.Stop();
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmGoods", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmGoods", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }
         public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
         {
             if (File.Exists(iniFilePath))
             {
                 long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                 if (OpStation == 0)
                 {
                     return false;
                 }
                 else
                 {
                     return true;
                 }
             }
             else
             {
                 return false;
             }
         }
         [DllImport("kernel32")]
         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

         private void timerGoods_Tick(object sender, EventArgs e)
         {
             getInfo();
         }
    }
}
